#include "MainWidget.h"
#include "ui_MainWidget.h"

#include <QDebug>
#include <QSettings>


#define CStr_IniFileName "ConfigsReg.ini"


MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    m_isSettingDef=false;

    initCfgRegMisc();

    ui->tabWidget->setCurrentIndex(0);
}

MainWidget::~MainWidget()
{
    delete ui;
}

///
/// \brief writeConfig1PageToIniFile
///将Config1页面的值写入ini文件
void MainWidget::writeConfig1PageToIniFile()
{
    if(!m_isSettingDef){
        QSettings setting(CStr_IniFileName,QSettings::IniFormat);
        setting.beginGroup("Config1");
        setting.setValue(ui->groupBoxSSMConfig->title(),ui->CB_SSMConfig->currentIndex());
        setting.setValue(ui->groupBox_FGOLSel->title(),ui->CB_FGOLSel->currentIndex());
        setting.setValue(ui->groupBox_FGCycle->title(),ui->CB_FGCycle->currentIndex());
        setting.setValue(ui->groupBox_ClkCycleAdjust->title(),ui->CB_ClkCycleAdjust->currentIndex());
        setting.setValue(ui->groupBox_RMResistance->title(),ui->CB_RMResistance->currentIndex());
        setting.endGroup();
    }
    updateConfig1RegRes();
}

///
/// \brief setConfig1PageToDef
///设置Config1页面默认值
void MainWidget::setConfig1PageToDef()
{
    m_isSettingDef=true;

    ui->CB_SSMConfig->setCurrentIndex(3);
    ui->CB_FGOLSel->setCurrentIndex(0);
    ui->CB_FGCycle->setCurrentIndex(0);
    ui->CB_ClkCycleAdjust->setCurrentIndex(0);
    ui->CB_RMResistance->setCurrentIndex(0);
    m_isSettingDef=false;

    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg1 = setting.contains("Config1/"+ui->groupBox_RMResistance->title());
    if(!hascfg1){
        writeConfig1PageToIniFile();
    }

    updateConfig1RegRes();
}

///
/// \brief readIniToConfig1Page
///读取ini文件的值到 Config1 page
void MainWidget::readIniToConfig1Page()
{
    m_isSettingDef=true;
    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg1 = setting.contains("Config1/"+ui->groupBox_RMResistance->title());
    if(hascfg1){
        setting.beginGroup("Config1");
        ui->CB_SSMConfig->setCurrentIndex(
                    setting.value(ui->groupBoxSSMConfig->title()).toInt()
                    );

        ui->CB_FGOLSel->setCurrentIndex(
                    setting.value(ui->groupBox_FGOLSel->title()).toInt()
                    );

        ui->CB_FGCycle->setCurrentIndex(
                    setting.value(ui->groupBox_FGCycle->title()).toInt()
                    );

        ui->CB_ClkCycleAdjust->setCurrentIndex(
                    setting.value(ui->groupBox_ClkCycleAdjust->title()).toInt()
                    );

        ui->CB_RMResistance->setCurrentIndex(
                    setting.value(ui->groupBox_RMResistance->title()).toInt()
                    );
        setting.endGroup();
    }
    m_isSettingDef=false;

    if(!hascfg1){
        setConfig1PageToDef();
    }
}

void MainWidget::writeConfig2PageToIniFile()
{
    if(!m_isSettingDef){
        QSettings setting(CStr_IniFileName,QSettings::IniFormat);
        setting.beginGroup("Config2");
        setting.setValue(ui->groupBox_BEMF_Kt->title(),ui->CB_BEMF_Kt->currentIndex());
        setting.setValue(ui->groupBox_CommAdvMode->title(),ui->CB_CommAdvMode->currentIndex());
        setting.setValue(ui->groupBox_TCtrlAdvTime->title(),ui->CB_TCtrlAdvTime->currentIndex());
        setting.endGroup();
    }
    updateConfig2RegRes();
}

void MainWidget::setConfig2PageToDef()
{
    m_isSettingDef=true;

    ui->CB_TCtrlAdvTime->setCurrentIndex(41);
    ui->CB_BEMF_Kt->setCurrentIndex(0);
    ui->CB_CommAdvMode->setCurrentIndex(0);

    m_isSettingDef=false;

    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config2/"+ui->groupBox_BEMF_Kt->title());
    if(!hascfg){
        writeConfig2PageToIniFile();
    }

    updateConfig2RegRes();
}

void MainWidget::readIniToConfig2Page()
{
    m_isSettingDef=true;
    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config2/"+ui->groupBox_BEMF_Kt->title());
    if(hascfg){
        setting.beginGroup("Config2");
        ui->CB_BEMF_Kt->setCurrentIndex(
                    setting.value(ui->groupBox_BEMF_Kt->title()).toInt()
                    );
        ui->CB_CommAdvMode->setCurrentIndex(
                    setting.value(ui->groupBox_CommAdvMode->title()).toInt()
                    );
        ui->CB_TCtrlAdvTime->setCurrentIndex(
                    setting.value(ui->groupBox_TCtrlAdvTime->title()).toInt()
                    );

        setting.endGroup();
    }

    m_isSettingDef=false;

    if(!hascfg){
        setConfig2PageToDef();
    }
}

void MainWidget::writeConfig3PageToIniFile()
{
    if(!m_isSettingDef){
        QSettings setting(CStr_IniFileName,QSettings::IniFormat);
        setting.beginGroup("Config3");
        setting.setValue(ui->groupBox_ISDThr->title(),ui->CB_ISDThr->currentIndex());
        setting.setValue(ui->groupBox_BrkCurThrSel->title(),ui->CB_BrkCurThrSel->currentIndex());
        setting.setValue(ui->groupBox_BEMF_HYS->title(),ui->CB_BEMF_HYS->currentIndex());
        setting.setValue(ui->groupBox_ISDEn->title(),ui->CB_ISDEn->currentIndex());
        setting.setValue(ui->groupBox_RvsDrEn->title(),ui->CB_RvsDrEn->currentIndex());
        setting.setValue(ui->groupBox_RvsDrThr->title(),ui->CB_RvsDrThr->currentIndex());
        setting.setValue(ui->groupBox_OpenLCurr->title(),ui->CB_OpenLCurr->currentIndex());
        setting.setValue(ui->groupBox_OpLCurrRt->title(),ui->CB_OpLCurrRt->currentIndex());
        setting.setValue(ui->groupBox_BrkDoneThr->title(),ui->CB_BrkDoneThr->currentIndex());
        setting.endGroup();
    }
    updateConfig3RegRes();
}

void MainWidget::setConfig3PageToDef()
{
    m_isSettingDef=true;

    ui->CB_ISDThr->setCurrentIndex(0);
    ui->CB_BrkCurThrSel->setCurrentIndex(0);
    ui->CB_BEMF_HYS->setCurrentIndex(0);
    ui->CB_ISDEn->setCurrentIndex(0);
    ui->CB_RvsDrEn->setCurrentIndex(0);
    ui->CB_RvsDrThr->setCurrentIndex(0);
    ui->CB_OpenLCurr->setCurrentIndex(3);
    ui->CB_OpLCurrRt->setCurrentIndex(0);
    ui->CB_BrkDoneThr->setCurrentIndex(1);

    m_isSettingDef=false;

    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config3/"+ui->groupBox_OpenLCurr->title());
    if(!hascfg){
        writeConfig3PageToIniFile();
    }

    updateConfig3RegRes();
}

void MainWidget::readIniToConfig3Page()
{
    m_isSettingDef=true;
    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config3/"+ui->groupBox_OpenLCurr->title());
    if(hascfg){
        setting.beginGroup("Config3");
        ui->CB_ISDThr->setCurrentIndex(
                    setting.value(ui->groupBox_ISDThr->title()).toInt()
                    );
        ui->CB_BrkCurThrSel->setCurrentIndex(
                    setting.value(ui->groupBox_BrkCurThrSel->title()).toInt()
                    );
        ui->CB_BEMF_HYS->setCurrentIndex(
                    setting.value(ui->groupBox_BEMF_HYS->title()).toInt()
                    );
        ui->CB_ISDEn->setCurrentIndex(
                    setting.value(ui->groupBox_ISDEn->title()).toInt()
                    );
        ui->CB_RvsDrEn->setCurrentIndex(
                    setting.value(ui->groupBox_RvsDrEn->title()).toInt()
                    );
        ui->CB_RvsDrThr->setCurrentIndex(
                    setting.value(ui->groupBox_RvsDrThr->title()).toInt()
                    );
        ui->CB_OpenLCurr->setCurrentIndex(
                    setting.value(ui->groupBox_OpenLCurr->title()).toInt()
                    );
        ui->CB_OpLCurrRt->setCurrentIndex(
                    setting.value(ui->groupBox_OpLCurrRt->title()).toInt()
                    );
        ui->CB_BrkDoneThr->setCurrentIndex(
                    setting.value(ui->groupBox_BrkDoneThr->title()).toInt()
                    );

        setting.endGroup();
    }

    m_isSettingDef=false;

    if(!hascfg){
        setConfig3PageToDef();
    }
}

void MainWidget::writeConfig4PageToIniFile()
{
    if(!m_isSettingDef){
        QSettings setting(CStr_IniFileName,QSettings::IniFormat);
        setting.beginGroup("Config4");
        setting.setValue(ui->groupBox_AccelRangeSel->title(),ui->CB_AccelRangeSel->currentIndex());
        setting.setValue(ui->groupBox_StAccel2->title(),ui->CB_StAccel2->currentIndex());
        setting.setValue(ui->groupBox_StAccel->title(),ui->CB_StAccel->currentIndex());
        setting.setValue(ui->groupBox_Op2ClsThr->title(),ui->CB_Op2ClsThr->currentIndex());
        setting.setValue(ui->groupBox_AlignTime->title(),ui->CB_AlignTime->currentIndex());
        setting.endGroup();
    }
    updateConfig4RegRes();
}

void MainWidget::setConfig4PageToDef()
{
    m_isSettingDef=true;

    ui->CB_AccelRangeSel->setCurrentIndex(0);
    ui->CB_StAccel2->setCurrentIndex(6);
    ui->CB_StAccel->setCurrentIndex(7);
    ui->CB_Op2ClsThr->setCurrentIndex(17);
    ui->CB_AlignTime->setCurrentIndex(0);

    m_isSettingDef=false;

    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config4/"+ui->groupBox_Op2ClsThr->title());
    if(!hascfg){
        writeConfig4PageToIniFile();
    }

    updateConfig4RegRes();
}

void MainWidget::readIniToConfig4Page()
{
    m_isSettingDef=true;
    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config4/"+ui->groupBox_Op2ClsThr->title());
    if(hascfg){
        setting.beginGroup("Config4");
        ui->CB_AccelRangeSel->setCurrentIndex(
                    setting.value(ui->groupBox_AccelRangeSel->title()).toInt()
                    );
        ui->CB_StAccel2->setCurrentIndex(
                    setting.value(ui->groupBox_StAccel2->title()).toInt()
                    );
        ui->CB_StAccel->setCurrentIndex(
                    setting.value(ui->groupBox_StAccel->title()).toInt()
                    );
        ui->CB_Op2ClsThr->setCurrentIndex(
                    setting.value(ui->groupBox_Op2ClsThr->title()).toInt()
                    );
        ui->CB_AlignTime->setCurrentIndex(
                    setting.value(ui->groupBox_AlignTime->title()).toInt()
                    );
        setting.endGroup();
    }

    m_isSettingDef=false;

    if(!hascfg){
        setConfig4PageToDef();
    }
}

void MainWidget::writeConfig5PageToIniFile()
{
    if(!m_isSettingDef){
        QSettings setting(CStr_IniFileName,QSettings::IniFormat);
        setting.beginGroup("Config5");
        setting.setValue(ui->groupBox_OTWarningLimit->title(),ui->CB_OTWarningLimit->currentIndex());
        setting.setValue(ui->groupBox_LockEn5->title(),ui->CB_LockEn5->currentIndex());
        setting.setValue(ui->groupBox_LockEn4->title(),ui->CB_LockEn4->currentIndex());
        setting.setValue(ui->groupBox_LockEn3->title(),ui->CB_LockEn3->currentIndex());
        setting.setValue(ui->groupBox_LockEn2->title(),ui->CB_LockEn2->currentIndex());
        setting.setValue(ui->groupBox_LockEn1->title(),ui->CB_LockEn1->currentIndex());
        setting.setValue(ui->groupBox_LockEn0->title(),ui->CB_LockEn0->currentIndex());
        setting.setValue(ui->groupBox_SWiLimitThr->title(),ui->CB_SWiLimitThr->currentIndex());
        setting.setValue(ui->groupBox_HWiLimitThr->title(),ui->CB_HWiLimitThr->currentIndex());
        setting.setValue(ui->groupBox_IPDasHwILimit->title(),ui->CB_IPDasHwILimit->currentIndex());
        setting.endGroup();
    }
    updateConfig5RegRes();
}

void MainWidget::setConfig5PageToDef()
{
    m_isSettingDef=true;

    ui->CB_OTWarningLimit->setCurrentIndex(0);
    ui->CB_LockEn5->setCurrentIndex(1);
    ui->CB_LockEn4->setCurrentIndex(1);
    ui->CB_LockEn3->setCurrentIndex(1);
    ui->CB_LockEn2->setCurrentIndex(0);
    ui->CB_LockEn1->setCurrentIndex(1);
    ui->CB_LockEn0->setCurrentIndex(1);
    ui->CB_SWiLimitThr->setCurrentIndex(10);
    ui->CB_HWiLimitThr->setCurrentIndex(7);
    ui->CB_IPDasHwILimit->setCurrentIndex(1);

    m_isSettingDef=false;

    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config5/"+ui->groupBox_LockEn4->title());
    if(!hascfg){
        writeConfig5PageToIniFile();
    }

    updateConfig5RegRes();
}

void MainWidget::readIniToConfig5Page()
{
    m_isSettingDef=true;
    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config5/"+ui->groupBox_LockEn4->title());
    if(hascfg){
        setting.beginGroup("Config5");
        ui->CB_OTWarningLimit->setCurrentIndex(
                    setting.value(ui->groupBox_OTWarningLimit->title()).toInt()
                    );
        ui->CB_LockEn5->setCurrentIndex(
                    setting.value(ui->groupBox_LockEn5->title()).toInt()
                    );
        ui->CB_LockEn4->setCurrentIndex(
                    setting.value(ui->groupBox_LockEn4->title()).toInt()
                    );
        ui->CB_LockEn3->setCurrentIndex(
                    setting.value(ui->groupBox_LockEn3->title()).toInt()
                    );
        ui->CB_LockEn2->setCurrentIndex(
                    setting.value(ui->groupBox_LockEn2->title()).toInt()
                    );
        ui->CB_LockEn1->setCurrentIndex(
                    setting.value(ui->groupBox_LockEn1->title()).toInt()
                    );
        ui->CB_LockEn0->setCurrentIndex(
                    setting.value(ui->groupBox_LockEn0->title()).toInt()
                    );
        ui->CB_SWiLimitThr->setCurrentIndex(
                    setting.value(ui->groupBox_SWiLimitThr->title()).toInt()
                    );
        ui->CB_HWiLimitThr->setCurrentIndex(
                    setting.value(ui->groupBox_HWiLimitThr->title()).toInt()
                    );
        ui->CB_IPDasHwILimit->setCurrentIndex(
                    setting.value(ui->groupBox_IPDasHwILimit->title()).toInt()
                    );

        setting.endGroup();
    }

    m_isSettingDef=false;

    if(!hascfg){
        setConfig5PageToDef();
    }
}

void MainWidget::writeConfig6PageToIniFile()
{
    if(!m_isSettingDef){
        QSettings setting(CStr_IniFileName,QSettings::IniFormat);
        setting.beginGroup("Config6");
        setting.setValue(ui->groupBox_SpdCtrlMd->title(),ui->CB_SpdCtrlMd->currentIndex());
        setting.setValue(ui->groupBox_PWMFreq->title(),ui->CB_PWMFreq->currentIndex());
        setting.setValue(ui->groupBox_KtLckThr->title(),ui->CB_KtLckThr->currentIndex());
        setting.setValue(ui->groupBox_AVSIndEn->title(),ui->CB_AVSIndEn->currentIndex());
        setting.setValue(ui->groupBox_AVSMEn->title(),ui->CB_AVSMEn->currentIndex());
        setting.setValue(ui->groupBox_AVSMMd->title(),ui->CB_AVSMMd->currentIndex());
        setting.setValue(ui->groupBox_IPDRlsMd->title(),ui->CB_IPDRlsMd->currentIndex());
        setting.setValue(ui->groupBox_CLoopDis->title(),ui->CB_CLoopDis->currentIndex());
        setting.setValue(ui->groupBox_ClsLpAccel->title(),ui->CB_ClsLpAccel->currentIndex());
        setting.setValue(ui->groupBox_DutyCycleLimit->title(),ui->CB_DutyCycleLimit->currentIndex());
        setting.setValue(ui->groupBox_SlewRate->title(),ui->CB_SlewRate->currentIndex());
        setting.endGroup();
    }
    updateConfig6RegRes();
}

void MainWidget::setConfig6PageToDef()
{
    m_isSettingDef=true;

    ui->CB_SpdCtrlMd->setCurrentIndex(0);
    ui->CB_PWMFreq->setCurrentIndex(1);
    ui->CB_KtLckThr->setCurrentIndex(3);
    ui->CB_AVSIndEn->setCurrentIndex(1);
    ui->CB_AVSMEn->setCurrentIndex(0);
    ui->CB_AVSMMd->setCurrentIndex(0);
    ui->CB_IPDRlsMd->setCurrentIndex(0);
    ui->CB_CLoopDis->setCurrentIndex(0);
    ui->CB_ClsLpAccel->setCurrentIndex(4);
    ui->CB_DutyCycleLimit->setCurrentIndex(0);
    ui->CB_SlewRate->setCurrentIndex(0);

    m_isSettingDef=false;

    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config6/"+ui->groupBox_SpdCtrlMd->title());
    if(!hascfg){
        writeConfig6PageToIniFile();
    }

    updateConfig6RegRes();
}

void MainWidget::readIniToConfig6Page()
{
    m_isSettingDef=true;
    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config6/"+ui->groupBox_SpdCtrlMd->title());
    if(hascfg){
        setting.beginGroup("Config6");
        ui->CB_SpdCtrlMd->setCurrentIndex(
                    setting.value(ui->groupBox_SpdCtrlMd->title()).toInt()
                    );
        ui->CB_PWMFreq->setCurrentIndex(
                    setting.value(ui->groupBox_PWMFreq->title()).toInt()
                    );
        ui->CB_KtLckThr->setCurrentIndex(
                    setting.value(ui->groupBox_KtLckThr->title()).toInt()
                    );
        ui->CB_AVSIndEn->setCurrentIndex(
                    setting.value(ui->groupBox_AVSIndEn->title()).toInt()
                    );
        ui->CB_AVSMEn->setCurrentIndex(
                    setting.value(ui->groupBox_AVSMEn->title()).toInt()
                    );
        ui->CB_AVSMMd->setCurrentIndex(
                    setting.value(ui->groupBox_AVSMMd->title()).toInt()
                    );
        ui->CB_IPDRlsMd->setCurrentIndex(
                    setting.value(ui->groupBox_IPDRlsMd->title()).toInt()
                    );
        ui->CB_CLoopDis->setCurrentIndex(
                    setting.value(ui->groupBox_CLoopDis->title()).toInt()
                    );
        ui->CB_ClsLpAccel->setCurrentIndex(
                    setting.value(ui->groupBox_ClsLpAccel->title()).toInt()
                    );
        ui->CB_DutyCycleLimit->setCurrentIndex(
                    setting.value(ui->groupBox_DutyCycleLimit->title()).toInt()
                    );
        ui->CB_SlewRate->setCurrentIndex(
                    setting.value(ui->groupBox_SlewRate->title()).toInt()
                    );

        setting.endGroup();
    }

    m_isSettingDef=false;

    if(!hascfg){
        setConfig6PageToDef();
    }
}

void MainWidget::writeConfig7PageToIniFile()
{
    if(!m_isSettingDef){
        QSettings setting(CStr_IniFileName,QSettings::IniFormat);
        setting.beginGroup("Config7");
        setting.setValue(ui->groupBox_IPDAdvcAg->title(),ui->CB_IPDAdvcAg->currentIndex());
        setting.setValue(ui->groupBox_IPDCurrThr->title(),ui->CB_IPDCurrThr->currentIndex());
        setting.setValue(ui->groupBox_IPDClk->title(),ui->CB_IPDClk->currentIndex());
        setting.setValue(ui->groupBox_CtrlCoef->title(),ui->CB_CtrlCoef->currentIndex());
        setting.setValue(ui->groupBox_DeadTime->title(),ui->CB_DeadTime->currentIndex());
        setting.endGroup();
    }
    updateConfig7RegRes();
}

void MainWidget::setConfig7PageToDef()
{
    m_isSettingDef=true;

    ui->CB_IPDAdvcAg->setCurrentIndex(0);
    ui->CB_IPDCurrThr->setCurrentIndex(0);
    ui->CB_IPDClk->setCurrentIndex(0);
    ui->CB_CtrlCoef->setCurrentIndex(3);
    ui->CB_DeadTime->setCurrentIndex(26);

    m_isSettingDef=false;

    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config7/"+ui->groupBox_DeadTime->title());
    if(!hascfg){
        writeConfig7PageToIniFile();
    }

    updateConfig7RegRes();
}

void MainWidget::readIniToConfig7Page()
{
    m_isSettingDef=true;
    QSettings setting(CStr_IniFileName,QSettings::IniFormat);
    bool hascfg = setting.contains("Config7/"+ui->groupBox_DeadTime->title());
    if(hascfg){
        setting.beginGroup("Config7");
        ui->CB_IPDAdvcAg->setCurrentIndex(
                    setting.value(ui->groupBox_IPDAdvcAg->title()).toInt()
                    );
        ui->CB_IPDCurrThr->setCurrentIndex(
                    setting.value(ui->groupBox_IPDCurrThr->title()).toInt()
                    );
        ui->CB_IPDClk->setCurrentIndex(
                    setting.value(ui->groupBox_IPDClk->title()).toInt()
                    );
        ui->CB_CtrlCoef->setCurrentIndex(
                    setting.value(ui->groupBox_CtrlCoef->title()).toInt()
                    );
        ui->CB_DeadTime->setCurrentIndex(
                    setting.value(ui->groupBox_DeadTime->title()).toInt()
                    );
        setting.endGroup();
    }

    m_isSettingDef=false;

    if(!hascfg){
        setConfig7PageToDef();
    }
}

///
/// \brief initCfgRegMisc
///初始化 界面寄存器相关控件
void MainWidget::initCfgRegMisc()
{
    initConfig1Page();
    initConfig2Page();
    initConfig3Page();
    initConfig4Page();
    initConfig5Page();
    initConfig6Page();
    initConfig7Page();
}

///
/// \brief initConfigRegPage
///初始化 Config1的页面
void MainWidget::initConfig1Page()
{
    QString str("FG / %1 (%2 pole)");
    for (int i = 1; i <= 16; ++i) {
        ui->CB_FGCycle->addItem(str.arg(i).arg(i*2));
    }

    str="0x%1 : %2";
    for (int i = 0; i < 128; ++i) {
        float v=0.00967f;
        int shift=i>>4;
        int index=i&0x0f;
        if(index<8 && shift!=0) continue;
        index=(index<<shift);
        v=index*v;
        ui->CB_RMResistance->addItem(str
                                     .arg(i,2,16,QChar('0'))
                                     .arg(static_cast<double>(v),0,'f',4,QChar('0'))
                                     );
        ui->CB_RMResistance->setItemData(ui->CB_RMResistance->count()-1,i);
    }

    readIniToConfig1Page();
    updateConfig1RegRes();

    connect(ui->CB_SSMConfig,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig1PageToIniFile())
            );
    connect(ui->CB_FGOLSel,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig1PageToIniFile())
            );
    connect(ui->CB_FGCycle,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig1PageToIniFile())
            );
    connect(ui->CB_ClkCycleAdjust,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig1PageToIniFile())
            );
    connect(ui->CB_RMResistance,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig1PageToIniFile())
            );

    connect(ui->btnCfg1SetDefault,SIGNAL(clicked()),
            this,SLOT(setConfig1PageToDef())
            );

    connect(ui->btnCfg1LastEdited,SIGNAL(clicked()),
            this,SLOT(readIniToConfig1Page())
            );
}

///
/// \brief initConfigRegPage
///初始化 Config2的页面
void MainWidget::initConfig2Page()
{
    QString str;

    str="0x%1 : %2";
    float v;
    for (int i = 0; i < 128; ++i) {

        int shift=i>>4;
        int index=i&0x0f;
        if(index<8 && shift!=0) continue;
        index=(index<<shift);
        v=0.92f;
        v=index*v;
        ui->CB_BEMF_Kt->addItem(str
                                .arg(i,2,16,QChar('0'))
                                .arg(static_cast<double>(v),0,'f',2,QChar('0'))
                                );
        ui->CB_BEMF_Kt->setItemData(ui->CB_BEMF_Kt->count()-1,i);

        v=2.5f;
        v=index*v;
        ui->CB_TCtrlAdvTime->addItem(str
                                     .arg(i,2,16,QChar('0'))
                                     .arg(static_cast<double>(v),0,'f',2,QChar('0'))
                                     );
        ui->CB_TCtrlAdvTime->setItemData(ui->CB_TCtrlAdvTime->count()-1,i);
    }

    readIniToConfig2Page();
    updateConfig2RegRes();

    connect(ui->CB_BEMF_Kt,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig2PageToIniFile())
            );
    connect(ui->CB_CommAdvMode,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig2PageToIniFile())
            );
    connect(ui->CB_TCtrlAdvTime,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig2PageToIniFile())
            );

    connect(ui->btnCfg2SetDefault,SIGNAL(clicked()),
            this,SLOT(setConfig2PageToDef())
            );

    connect(ui->btnCfg2LastEdited,SIGNAL(clicked()),
            this,SLOT(readIniToConfig2Page())
            );
}

///
/// \brief initConfigRegPage
///初始化 Config3的页面
void MainWidget::initConfig3Page()
{
    readIniToConfig3Page();
    updateConfig3RegRes();

    connect(ui->CB_ISDThr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );
    connect(ui->CB_BrkCurThrSel,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );
    connect(ui->CB_BEMF_HYS,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );
    connect(ui->CB_ISDEn,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );
    connect(ui->CB_RvsDrEn,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );
    connect(ui->CB_RvsDrThr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );
    connect(ui->CB_OpenLCurr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );
    connect(ui->CB_OpLCurrRt,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );
    connect(ui->CB_BrkDoneThr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig3PageToIniFile())
            );

    connect(ui->btnCfg3SetDefault,SIGNAL(clicked()),
            this,SLOT(setConfig3PageToDef())
            );

    connect(ui->btnCfg3LastEdited,SIGNAL(clicked()),
            this,SLOT(readIniToConfig3Page())
            );
}

///
/// \brief initConfigRegPage
///初始化 Config4的页面
void MainWidget::initConfig4Page()
{
    QString str="0x%1 : %2 Hz";
    for (int i = 0; i < 32; ++i) {
        if(i<16){
            ui->CB_Op2ClsThr->addItem(str
                                      .arg(i,2,16,QChar('0'))
                                      .arg(0.8*i,0,'f',1,QChar('0'))
                                      );
        }else{
            ui->CB_Op2ClsThr->addItem(str
                                      .arg(i,2,16,QChar('0'))
                                      .arg(12.8*((i & 0x0f) + 1),0,'f',1,QChar('0'))
                                      );
        }
    }

    readIniToConfig4Page();
    updateConfig4RegRes();

    connect(ui->CB_AccelRangeSel,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig4PageToIniFile())
            );
    connect(ui->CB_StAccel2,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig4PageToIniFile())
            );
    connect(ui->CB_StAccel,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig4PageToIniFile())
            );
    connect(ui->CB_Op2ClsThr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig4PageToIniFile())
            );
    connect(ui->CB_AlignTime,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig4PageToIniFile())
            );

    connect(ui->btnCfg4SetDefault,SIGNAL(clicked()),
            this,SLOT(setConfig4PageToDef())
            );

    connect(ui->btnCfg4LastEdited,SIGNAL(clicked()),
            this,SLOT(readIniToConfig4Page())
            );
}

///
/// \brief initConfigRegPage
///初始化 Config5的页面
void MainWidget::initConfig5Page()
{
    ui->CB_SWiLimitThr->addItem(tr("No software current limit"));
    QString str="%1 A";
    for (int i = 1; i < 16; ++i) {
        ui->CB_SWiLimitThr->addItem(str
                                    .arg(0.2*i,0,'f',1,QChar('0'))
                                    );
    }

    readIniToConfig5Page();
    updateConfig5RegRes();

    connect(ui->CB_OTWarningLimit,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_LockEn5,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_LockEn4,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_LockEn3,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_LockEn2,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_LockEn1,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_LockEn0,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_SWiLimitThr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_HWiLimitThr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );
    connect(ui->CB_IPDasHwILimit,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig5PageToIniFile())
            );


    connect(ui->btnCfg5SetDefault,SIGNAL(clicked()),
            this,SLOT(setConfig5PageToDef())
            );

    connect(ui->btnCfg5LastEdited,SIGNAL(clicked()),
            this,SLOT(readIniToConfig5Page())
            );
}

///
/// \brief initConfigRegPage
///初始化 Config6的页面
void MainWidget::initConfig6Page()
{
    readIniToConfig6Page();
    updateConfig6RegRes();

    connect(ui->CB_SpdCtrlMd,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_PWMFreq,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_KtLckThr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_AVSIndEn,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_AVSMEn,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_AVSMMd,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_IPDRlsMd,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_CLoopDis,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_ClsLpAccel,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_DutyCycleLimit,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );
    connect(ui->CB_SlewRate,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig6PageToIniFile())
            );


    connect(ui->btnCfg6SetDefault,SIGNAL(clicked()),
            this,SLOT(setConfig6PageToDef())
            );

    connect(ui->btnCfg6LastEdited,SIGNAL(clicked()),
            this,SLOT(readIniToConfig6Page())
            );
}

///
/// \brief initConfigRegPage
///初始化 Config7的页面
void MainWidget::initConfig7Page()
{
    ui->CB_IPDCurrThr->addItem("No IPD function. Align and go");
    ui->CB_IPDCurrThr->addItem("0.4 A current threshold");
    QString str="%1 A current threshold";
    for (int i = 2; i < 16; ++i) {
        ui->CB_IPDCurrThr->addItem(str
                                   .arg(0.2*(i+1),0,'f',1,QChar('0'))
                                   );
    }

    str="%1 ns";
    for (int i = 1; i <= 32; ++i) {
        ui->CB_DeadTime->addItem(str
                                 .arg(i*40)
                                 );
    }

    readIniToConfig7Page();
    updateConfig7RegRes();

    connect(ui->CB_IPDAdvcAg,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig7PageToIniFile())
            );
    connect(ui->CB_IPDCurrThr,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig7PageToIniFile())
            );
    connect(ui->CB_IPDClk,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig7PageToIniFile())
            );
    connect(ui->CB_CtrlCoef,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig7PageToIniFile())
            );
    connect(ui->CB_DeadTime,SIGNAL(currentIndexChanged(int)),
            this,SLOT(writeConfig7PageToIniFile())
            );

    connect(ui->btnCfg7SetDefault,SIGNAL(clicked()),
            this,SLOT(setConfig7PageToDef())
            );

    connect(ui->btnCfg7LastEdited,SIGNAL(clicked()),
            this,SLOT(readIniToConfig7Page())
            );
}

///
/// \brief updateConfig1RegRes
///更新配置寄存1 的显示结果
void MainWidget::updateConfig1RegRes()
{
    int reg=ui->CB_RMResistance->itemData(ui->CB_RMResistance->currentIndex()).toInt();
    reg|=ui->CB_SSMConfig->currentIndex()<<14;
    reg|=ui->CB_FGOLSel->currentIndex()<<12;
    reg|=ui->CB_FGCycle->currentIndex()<<8;
    reg|=ui->CB_ClkCycleAdjust->currentIndex()<<7;

    ui->Cfg1ResHex->setText(
                QString("%1").arg(reg,4,16,QChar('0')).toUpper()
                );
    ui->Cfg1ResDec->setText(
                QString("%1").arg(reg)
                );
}

void MainWidget::updateConfig2RegRes()
{
    int reg=ui->CB_TCtrlAdvTime->itemData(ui->CB_TCtrlAdvTime->currentIndex()).toInt();
    reg|=ui->CB_CommAdvMode->currentIndex()<<7;
    reg|=ui->CB_BEMF_Kt->itemData(ui->CB_BEMF_Kt->currentIndex()).toInt()<<8;

    ui->Cfg2ResHex->setText(
                QString("%1").arg(reg,4,16,QChar('0')).toUpper()
                );
    ui->Cfg2ResDec->setText(
                QString("%1").arg(reg)
                );
}

void MainWidget::updateConfig3RegRes()
{
    int reg=ui->CB_BrkDoneThr->currentIndex();
    reg|=ui->CB_ISDThr->currentIndex()<<14;
    reg|=ui->CB_BrkCurThrSel->currentIndex()<<13;
    reg|=ui->CB_BEMF_HYS->currentIndex()<<12;
    reg|=ui->CB_ISDEn->currentIndex()<<11;
    reg|=ui->CB_RvsDrEn->currentIndex()<<10;
    reg|=ui->CB_RvsDrThr->currentIndex()<<8;
    reg|=ui->CB_OpenLCurr->currentIndex()<<6;
    reg|=ui->CB_OpLCurrRt->currentIndex()<<3;

    ui->Cfg3ResHex->setText(
                QString("%1").arg(reg,4,16,QChar('0')).toUpper()
                );
    ui->Cfg3ResDec->setText(
                QString("%1").arg(reg)
                );
}

void MainWidget::updateConfig4RegRes()
{
    int reg=ui->CB_AlignTime->currentIndex();
    reg|=ui->CB_AccelRangeSel->currentIndex()<<14;
    reg|=ui->CB_StAccel2->currentIndex()<<11;
    reg|=ui->CB_StAccel->currentIndex()<<8;
    reg|=ui->CB_Op2ClsThr->currentIndex()<<3;


    ui->Cfg4ResHex->setText(
                QString("%1").arg(reg,4,16,QChar('0')).toUpper()
                );
    ui->Cfg4ResDec->setText(
                QString("%1").arg(reg)
                );
}

void MainWidget::updateConfig5RegRes()
{
    int reg=ui->CB_IPDasHwILimit->currentIndex();
    reg|=ui->CB_OTWarningLimit->currentIndex()<<14;
    reg|=ui->CB_LockEn5->currentIndex()<<13;
    reg|=ui->CB_LockEn4->currentIndex()<<12;
    reg|=ui->CB_LockEn3->currentIndex()<<11;
    reg|=ui->CB_LockEn2->currentIndex()<<10;
    reg|=ui->CB_LockEn1->currentIndex()<<9;
    reg|=ui->CB_LockEn0->currentIndex()<<8;
    reg|=ui->CB_SWiLimitThr->currentIndex()<<4;
    reg|=ui->CB_HWiLimitThr->currentIndex()<<1;

    ui->Cfg5ResHex->setText(
                QString("%1").arg(reg,4,16,QChar('0')).toUpper()
                );
    ui->Cfg5ResDec->setText(
                QString("%1").arg(reg)
                );
}

void MainWidget::updateConfig6RegRes()
{
    int reg=ui->CB_SlewRate->currentIndex();
    reg|=ui->CB_SpdCtrlMd->currentIndex()<<15;
    reg|=ui->CB_PWMFreq->currentIndex()<<14;
    reg|=ui->CB_KtLckThr->currentIndex()<<12;
    reg|=ui->CB_AVSIndEn->currentIndex()<<11;
    reg|=ui->CB_AVSMEn->currentIndex()<<10;
    reg|=ui->CB_AVSMMd->currentIndex()<<9;
    reg|=ui->CB_IPDRlsMd->currentIndex()<<8;
    reg|=ui->CB_CLoopDis->currentIndex()<<7;
    reg|=ui->CB_ClsLpAccel->currentIndex()<<4;
    reg|=ui->CB_DutyCycleLimit->currentIndex()<<2;

    ui->Cfg6ResHex->setText(
                QString("%1").arg(reg,4,16,QChar('0')).toUpper()
                );
    ui->Cfg6ResDec->setText(
                QString("%1").arg(reg)
                );
}

void MainWidget::updateConfig7RegRes()
{
    int reg=ui->CB_DeadTime->currentIndex();
    reg|=ui->CB_IPDAdvcAg->currentIndex()<<14;
    reg|=ui->CB_IPDCurrThr->currentIndex()<<10;
    reg|=ui->CB_IPDClk->currentIndex()<<8;
    reg|=ui->CB_CtrlCoef->currentIndex()<<5;

    ui->Cfg7ResHex->setText(
                QString("%1").arg(reg,4,16,QChar('0')).toUpper()
                );
    ui->Cfg7ResDec->setText(
                QString("%1").arg(reg)
                );
}

void MainWidget::on_tabWidget_currentChanged(int index)
{
    Q_UNUSED(index);
    if(ui->tabWidget->currentWidget() == ui->tabSummary){
        ui->SumaryCfg1->setText(ui->Cfg1ResHex->text());
        ui->SumaryCfg2->setText(ui->Cfg2ResHex->text());
        ui->SumaryCfg3->setText(ui->Cfg3ResHex->text());
        ui->SumaryCfg4->setText(ui->Cfg4ResHex->text());
        ui->SumaryCfg5->setText(ui->Cfg5ResHex->text());
        ui->SumaryCfg6->setText(ui->Cfg6ResHex->text());
        ui->SumaryCfg7->setText(ui->Cfg7ResHex->text());
    }
}
